/**
 * @title: NacosManagerUtil
 * @projectName mscloud
 * @Description TODO
 * @Author
 * @Date 2022/3/3 9:43
 */
package com.atguigu.springcloud.cloudsentinelgatewaydevelop.nacos;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.Result;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.vo.InstanceVO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.config.NacosConfigPorperties;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.config.NacosServicePorperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Data
@EnableConfigurationProperties({
        NacosServicePorperties.class,
        NacosConfigPorperties.class
})
@Component
public class NacosManagerUtil {
    @Autowired
    NacosServicePorperties nacosServicePorperties;
    @Autowired
    NacosConfigPorperties nacosConfigPorperties;
    @Autowired
    RestTemplate restTemplate;


    public ResponseEntity<String> deRegisterService(String serviceName){
        String url="http://"+nacosServicePorperties.getServerAddr() + "/nacos/v1/ns/service";
        url=url+"?serviceName="+serviceName+"&namespaceId="+nacosServicePorperties.getNamespace()+"&groupName="+nacosServicePorperties.getGroup();
        Map<String,String> map=new HashMap<>();
//        map.put("serviceName",serviceName);
//        map.put("namespaceId",nacosServicePorperties.getNamespace());
//        map.put("groupName",nacosServicePorperties.getGroup());
        HttpEntity entity = new HttpEntity<>(map);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class);
        return exchange;
    }



    public ResponseEntity<String> doCreateService(String serviceName, String protectThreshold, String metadata, String selector){
        String url="http://"+nacosServicePorperties.getServerAddr() + "/nacos/v1/ns/service";
        Map<String,String> map=new HashMap<>();
//        map.put("serviceName",serviceName);
//        map.put("protectThreshold",protectThreshold);
//        map.put("metadata",metadata);
//        map.put("groupName",nacosServicePorperties.getGroup());
//        map.put("namespaceId",nacosServicePorperties.getNamespace());
//        if(!StringUtils.isEmpty(selector)){
//            map.put("selector",selector);
//        }
        url=url+"?serviceName="+serviceName+"&protectThreshold="+protectThreshold+"&metadata="+metadata+(StringUtils.isEmpty(selector)?"":("&selector="+selector));
        HttpEntity httpEntity= new HttpEntity<>(map);
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        return result;
    }

    public ResponseEntity<String> doDeRegisterInstance(String serviceName, String ip, int port, Boolean ephemeral){
        String url="http://"+nacosServicePorperties.getServerAddr() + "/nacos/v1/ns/instance";
        url=url+"?serviceName="+serviceName+"&ip="+ip+"&port="+port+"&ephemeral="+ephemeral;
        Map<String,String> map=new HashMap<>();
//        map.put("serviceName",serviceName);
//        map.put("ip",ip);
//        map.put("port",port+"");
//        map.put("ephemeral",ephemeral.toString());
        HttpEntity httpEntity=new HttpEntity(map);
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, String.class);
       return result;

    }

    public ResponseEntity<JSONObject> doGetServiceDetail(String serviceName){
        String url="http://"+nacosServicePorperties.getServerAddr() + "/nacos/v1/ns/service?serviceName=" + serviceName + "&groupName=" + nacosServicePorperties.getGroup() + "&namespaceId=" + nacosServicePorperties.getNamespace();
        ResponseEntity<JSONObject> ret = restTemplate.getForEntity(url, JSONObject.class);
        return ret;
    }

    public ResponseEntity<String> doUpDateService(String serviceName, String protectThreshold, String metadata, String selector) {
        String url="http://"+nacosServicePorperties.getServerAddr() + "/nacos/v1/ns/service?serviceName="+serviceName
                +"&groupName=" + nacosServicePorperties.getGroup()
                +"&namespaceId=" + nacosServicePorperties.getNamespace()
                +"&protectThreshold="+protectThreshold
                +"&metadata="+metadata
                +"&selector"+selector;
        Map params=new HashMap();
        HttpEntity httpEntity=new HttpEntity(params);
        return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, String.class);
    }
    //http://localhost:8848/nacos/v1/cs/configs?dataId=&group=&pageNo=1&pageSize=10&tenant=&search=accurate
    public ResponseEntity<JSONObject> doGetAllConfig(int pageNo,int pageSize) {
        String url="http://"+nacosConfigPorperties.getServerAddr()+"/nacos/v1/cs/configs?dataId="
                +"&group="+nacosConfigPorperties.getGroup()
                +"&tenant="+nacosConfigPorperties.getNamespace()
                +"&pageNo="+pageNo
                +"&pageSize="+pageSize
                +"&search=accurate";
        Map params=new HashMap();
        HttpEntity httpEntity=new HttpEntity(params);
        return restTemplate.exchange(url,HttpMethod.GET,httpEntity,JSONObject.class);
    }


    public ResponseEntity<String> doUpDateInstance(InstanceVO instance) {
        String url="http://"+nacosServicePorperties.getServerAddr()+
                "/nacos/v1/ns/instance?serviceName="+instance.getServiceName()+
                "&groupName="+nacosServicePorperties.getGroup()+
                "&namespaceId="+nacosServicePorperties.getNamespace()+
                "&ip="+instance.getIp()+
                "&port="+instance.getPort()+
                "&weight="+instance.getWeight()+
                "&metadata="+ JSONArray.toJSONString(instance.getMetadata())+
                "&enabled="+instance.isEnabled()+
                "ephemeral="+instance.isEphemeral();
        Map params=new HashMap();
        HttpEntity httpEntity=new HttpEntity(params);
        return restTemplate.exchange(url,HttpMethod.PUT,httpEntity,String.class);
    }
}
